package com.yssoft.service.purchase;

import com.yssoft.entity.core.TDdDetail;
import com.yssoft.entity.purchase.TDemandDetail;
import com.yssoft.entity.purchase.TDemandInfo;
import com.yssoft.entity.purchase.TPurchaseInfo;
import com.yssoft.mapper.core.TAccountsMapper;
import com.yssoft.mapper.core.TDdDetailMapper;
import com.yssoft.mapper.core.TDepartmentMapper;
import com.yssoft.mapper.core.TMaterialMapper;
import com.yssoft.mapper.core.TPersonnelMapper;
import com.yssoft.mapper.purchase.TDemandDetailMapper;
import com.yssoft.mapper.purchase.TDemandInfoMapper;
import com.yssoft.mapper.purchase.TPurchaseAttMapper;
import com.yssoft.mapper.purchase.TPurchaseDetailMapper;
import com.yssoft.mapper.purchase.TPurchaseInfoMapper;
import com.yssoft.mapper.purchase.TRecipientDetailMapper;
import com.yssoft.utils.AppUserHelper;
import com.yssoft.vo.TDemandDetailVo;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈翼东
 * @说明 申请单service
 * @time 2018/4/29 下午 05:37
 */
@Service
public class DemandInfoService {

    @Resource
    private TDemandInfoMapper demandInfoMapper;

    @Resource
    private TDemandDetailMapper demandDetailMapper;

    @Resource
    private TDdDetailMapper ddDetailMapper;

    @Resource
    private TMaterialMapper materialMapper;

    @Resource
    private TAccountsMapper accountsMapper;

    @Resource
    private TPersonnelMapper personnelMapper;

    @Resource
    private TPurchaseDetailMapper purchaseDetailMapper;

    @Resource
    private TPurchaseInfoMapper purchaseInfoMapper;

    @Resource
    private TRecipientDetailMapper recipientDetailMapper;

    @Resource
    private TPurchaseAttMapper purchaseAttMapper;

    @Resource
    private TDepartmentMapper departmentMapper;


    public List<Map<String, Object>> findPageByApplied(int page, int limit, int companyId, String goodsClassifyId, String goodsName) {
        List<Map<String, Object>> demandInfoList = null;
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("companyId", companyId);
            List<TDdDetail> detailList = null;
            if (goodsClassifyId != null && goodsClassifyId.length() > 0) {
                detailList = ddDetailMapper.queryChildrenDdDetail(Integer.parseInt(goodsClassifyId));
            } else {
                param.put("goodsClassifyId", null);
            }
            if (detailList != null && detailList.size() > 0) {
                param.put("goodsClassifyId", detailList);
            } else {
                param.put("goodsClassifyId", null);
            }

            param.put("goodsName", goodsName);
            param.put("page", page * limit);
            param.put("countNum", limit);
            demandInfoList = demandInfoMapper.selectByUserId(param);
            for (Map<String, Object> demandInfo : demandInfoList) {
                Integer infoId = Integer.parseInt(demandInfo.get("info_id").toString());
                List<Map<String, Object>> detail = demandDetailMapper.selectByInfoId(infoId);
                demandInfo.put("purchaseRequestDetails", detail);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return demandInfoList;

    }

    /**
     * @param page         当前页数
     * @param limit        每页条数
     * @param companyId    企业id
     * @param requestDate  申请时间
     * @param requestState 状态
     * @return List<TDemandInfo>
     * @方法名称: findPage
     * @功能描述: 获取申请单
     * @作者: 洪丰
     * @创建时间:2018年5月2日 上午11:14:44
     */
    public List<TDemandInfo> findPage(int page, int limit, int companyId, String requestDate, String requestState) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("requestDate", requestDate);
        param.put("requestState", requestState);
        param.put("companyId", companyId);
        int start = ((page - 1) * limit);
        int end = limit;
        param.put("start", start);
        param.put("end", end);
        List<TDemandInfo> demandInfoList = demandInfoMapper.selectDemandInfoAndDetail(param);
        return demandInfoList;
    }

    /**
     * @param id 申请单id
     * @return Map<String, Object>
     * @方法名称: findOne
     * @功能描述: 根据申请id获取申请单信息
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午2:12:21
     */
    public Map<String, Object> findOne(Integer id) {
        TDemandInfo dEntity = demandInfoMapper.selectByPrimaryKey(id);
        List<Map<String, Object>> demandDetailList = demandInfoMapper.selectOneDemandInfo(id);
        Map<String, Object> demandMap = new HashMap<>();
        demandMap.put("Tdemand", dEntity);
        demandMap.put("TdemandDetail", demandDetailList);
        return demandMap;
    }

    /**
     * @param id void
     * @方法名称: deleteOne
     * @功能描述: 删除请求单以及详细
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午2:36:27
     */
    public void deleteOne(Integer id) {
        TDemandInfo dEntity = new TDemandInfo();
        dEntity.setId(id);
        dEntity.setDeleteFlag("1");
        demandInfoMapper.updateByPrimaryKeySelective(dEntity);

        TDemandDetail ddEntity = new TDemandDetail();
        ddEntity.settDId(id);
        ddEntity.setDeleteFlag("1");
        demandDetailMapper.deleteByDemandInfoId(ddEntity);
    }

    /**
     * @return List<Map < String, Object>>
     * @方法名称: findMaterialType
     * @功能描述: 获取商品父类
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午3:56:52
     */
    public List<Map<String, Object>> findMaterialType() {
        List<Map<String, Object>> materialClassilylList = ddDetailMapper.selectMaterialDdDetail();
        return materialClassilylList;
    }

    /**
     * @param pId
     * @return List<TDdDetail>
     * @方法名称: findChildrenMaterialClassily
     * @功能描述: 根据父类id获取子类
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午4:01:01
     */
    public List<TDdDetail> findChildrenMaterialType(Integer pId, Integer entId) {
        TDdDetail dEntity = new TDdDetail();
        dEntity.settDId(pId);
        dEntity.setDeleteFlag("0");
        dEntity.settEId(entId);

        List<TDdDetail> materialClassilylList = ddDetailMapper.select(dEntity);
        return materialClassilylList;
    }

    /**
     * @param page       当前页数
     * @param limit      每页条数
     * @param companyId  企业id
     * @param classifyId 类型id
     * @return List<Map < String, Object>>
     * @方法名称: findMaterialByType
     * @功能描述: 根据类别获取物料
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午4:35:16
     */
    public List<Map<String, Object>> findMaterialByType(int page, int limit, int companyId, String classifyId, String materialName) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("classifyId", classifyId);
        param.put("companyId", companyId);
        param.put("materialName", materialName);
        int start = ((page - 1) * limit);
        int end = limit;
        param.put("start", start);
        param.put("end", end);
        List<Map<String, Object>> materialList = materialMapper.selectMaterialByType(param);
        return materialList;
    }

    /**
     * @param id   申请单id
     * @param stat 申请单状态
     * @return Integer 更新条数
     * @方法名称: updateStat
     * @功能描述: 修改申请单状态
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午4:51:00
     */
    public Integer updateStat(int id, String stat) {
        TDemandInfo dEntity = new TDemandInfo();
        dEntity.setId(id);
        dEntity.setStat(stat);
        int result = demandInfoMapper.updateByPrimaryKeySelective(dEntity);
        return result;
    }

    /**
     * @param id   申请详细id
     * @param stat 申请状态
     * @return Integer 更新条数
     * @方法名称: updateDetailStat
     * @功能描述: 修改申请详细状态
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午4:54:20
     */
    public Integer updateDetailStat(int id, String stat) {
        TDemandDetail ddEntity = new TDemandDetail();
        ddEntity.setId(id);
        ddEntity.setStat(stat);
        int result = demandDetailMapper.updateByPrimaryKeySelective(ddEntity);
        return result;
    }

    /**
     * @param userId
     * @param id
     * @param state
     * @param detailList void
     * @方法名称: save
     * @功能描述: 保存/编辑申请单及其详细
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午6:41:50
     */
    public void save(HttpServletRequest request, TDemandInfo entity) {

        String code = "S" + Calendar.getInstance().getTimeInMillis();
        if (null == entity.getId() || "".equals(entity.getId())) {
            entity.setDeleteFlag("0");
            entity.setCode(code);
            entity.setCreateTime(new Date());
            entity.setCreatorId(AppUserHelper.getCurrentUserId(request));
            entity.setCreatorName(AppUserHelper.getCurrentUserName(request));
            entity.settEId(AppUserHelper.getCurrentCompanyId(request));
            entity.setDeptId(AppUserHelper.getCurrentDeptId(request));
            entity.setDeptName(AppUserHelper.getCurrentDeptName(request));
            entity.setStat(entity.getStat());
            demandInfoMapper.insert(entity);
            int i = 1;
            for (TDemandDetailVo detailEntityVo : entity.getDetailList()) {
                if (null == detailEntityVo.gettMId() || "".equals(detailEntityVo.gettMId())) {
                    continue;
                }
                TDemandDetail detailEntity = new TDemandDetail();
                detailEntity.settDId(entity.getId());
                detailEntity.setCode(code + i);
                detailEntity.setStat("1");
                detailEntity.settEId(AppUserHelper.getCurrentCompanyId(request));

                detailEntity.setDeleteFlag("0");
                detailEntity.setCreateTime(new Date());
                detailEntity.setCreatorId(AppUserHelper.getCurrentUserId(request));
                detailEntity.setCreatorName(AppUserHelper.getCurrentUserName(request));
                detailEntity.settEId(AppUserHelper.getCurrentCompanyId(request));
                detailEntity.setDeptId(AppUserHelper.getCurrentDeptId(request));
                detailEntity.setDeptName(AppUserHelper.getCurrentDeptName(request));

                detailEntity.setQuantity(detailEntityVo.getQuantity());
                detailEntity.setRemarks(detailEntityVo.getRemarks());
                detailEntity.setSupplyTime(detailEntityVo.getSupplyTime());
                detailEntity.settMId(detailEntityVo.gettMId());

                demandDetailMapper.insert(detailEntity);
                i++;
            }
        } else {
            //删除原有详细
            TDemandDetail ddEntity = new TDemandDetail();
            ddEntity.settDId(entity.getId());
            ddEntity.setDeleteFlag("1");
            demandDetailMapper.deleteByDemandInfoId(ddEntity);

            int i = 1;
            for (TDemandDetailVo detailEntityVo : entity.getDetailList()) {
                if (null == detailEntityVo.gettMId() || "".equals(detailEntityVo.gettMId())) {
                    continue;
                }
                TDemandDetail detailEntity = new TDemandDetail();
                detailEntity.settDId(entity.getId());
                detailEntity.setCode(code + i);
                detailEntity.setStat("1");
                detailEntity.settEId(AppUserHelper.getCurrentCompanyId(request));

                detailEntity.setDeleteFlag("0");
                detailEntity.setCreateTime(new Date());
                detailEntity.setCreatorId(AppUserHelper.getCurrentUserId(request));
                detailEntity.setCreatorName(AppUserHelper.getCurrentUserName(request));
                detailEntity.settEId(AppUserHelper.getCurrentCompanyId(request));
                detailEntity.setDeptId(AppUserHelper.getCurrentDeptId(request));
                detailEntity.setDeptName(AppUserHelper.getCurrentDeptName(request));

                detailEntity.setQuantity(detailEntityVo.getQuantity());
                detailEntity.setRemarks(detailEntityVo.getRemarks());
                detailEntity.setSupplyTime(detailEntityVo.getSupplyTime());
                detailEntity.settMId(detailEntityVo.gettMId());

                demandDetailMapper.insert(detailEntity);
                i++;
            }
        }
    }

    /**
     * @param demandDetailId 申请单明细id
     * @return Map<String, Object>
     * @方法名称: findPurchaseDetail
     * @功能描述: 根据申请明细id查看采购单明细信息
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午7:50:49
     */
    public Map<String, Object> findPurchaseDetail(int demandDetailId) {
        Map<String, Object> purchaseMap = new HashMap<>();

        Map<String, Object> purDetail = purchaseDetailMapper.selectByDemandDetailId(demandDetailId);
        purchaseMap.put("purDetail", purDetail);

        if (null != purDetail && null != purDetail.get("t_p_id")) {
            TPurchaseInfo purInfo = purchaseInfoMapper.selectByPrimaryKey(Integer.parseInt(purDetail.get("t_p_id").toString()));
            purchaseMap.put("purInfo", purInfo);
        }
        return purchaseMap;
    }

    /**
     * @param purchaseDetailId
     * @return Map<String, Object>
     * @方法名称: findRecipientDetail
     * @功能描述: 根据采购明细id获取收货详情
     * @作者: 洪丰
     * @创建时间:2018年5月2日 下午8:29:48
     */
    public Map<String, Object> findRecipientDetail(int purchaseDetailId) {
        Map<String, Object> purchaseMap = new HashMap<>();

        Map<String, Object> purDetail = purchaseDetailMapper.selectByPurchaseDetailId(purchaseDetailId);
        purchaseMap.put("purDetail", purDetail);
        if (null != purDetail) {
            List<Map<String, Object>> recipientList = recipientDetailMapper.selectByPurchaseDetailId(purchaseDetailId);
            for (Map<String, Object> recivetMap : recipientList) {
                List<Map<String, Object>> att = purchaseAttMapper.selectByReciveId(Integer.parseInt(recivetMap.get("id") + ""));
                recivetMap.put("att", att);
            }
            purchaseMap.put("recipientList", recipientList);
        }
        return purchaseMap;
    }

    /**
     * @param ids void
     * @方法名称: deleteMore
     * @功能描述: 批量删除
     * @作者: 洪丰
     * @创建时间:2018年5月3日 上午9:42:08
     */
    public void deleteMore(String[] ids) {
        for (String id : ids) {
            TDemandInfo dEntity = new TDemandInfo();
            dEntity.setId(Integer.parseInt(id));
            dEntity.setDeleteFlag("1");
            demandInfoMapper.updateByPrimaryKeySelective(dEntity);

            TDemandDetail ddEntity = new TDemandDetail();
            ddEntity.settDId(Integer.parseInt(id));
            ddEntity.setDeleteFlag("1");
            demandDetailMapper.deleteByDemandInfoId(ddEntity);
        }
    }
}


